home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’92 / PatchWorks Kit / <PatchWorks++> / Exceptions.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-30  |  1.0 KB  |  53 lines  |  [TEXT/KAHL]

  1. /*
  2.     Exceptions.h
  3.     
  4.     C++ style exception handling.
  5.     
  6.     Part of PatchWorks, the Extension Development Framework.
  7.     
  8.     by Mouse Herrell & Patrick Beard.
  9.     
  10.     Permission is granted to use this source code for any purpose, as long
  11.     as the copyright notice is maintained.
  12.     
  13.     © 1992 Berkeley Systems, Inc.
  14.  */
  15.  
  16. #pragma once
  17.  
  18. #ifndef __EXCEPTIONS__
  19. #define __EXCEPTIONS__
  20.  
  21. #include <setjmp.h>
  22.  
  23. struct ExceptionFrame {
  24.     jmp_buf frame;
  25.     struct ExceptionFrame* next;
  26. };
  27.  
  28. typedef struct ExceptionFrame ExceptionFrame;
  29.  
  30. extern int theException;
  31. extern ExceptionFrame* theFrame;
  32.  
  33. #define throw(value) longjmp(theFrame->frame, value)
  34.  
  35. #define try \
  36.     {    ExceptionFrame exception; \
  37.         exception.next = theFrame; \
  38.         theFrame = &exception; \
  39.         if (!(theException = setjmp(theFrame->frame))) { \
  40.  
  41. #define catch \
  42.         } \
  43.         theFrame = theFrame->next; \
  44.     } \
  45.     for (; theException != 0; theException = 0)
  46.  
  47. // convenience macros.
  48.  
  49. #define FailNil(p) do { if (!p) throw(memFullErr); } while (0)
  50. #define FailErr(e) do { if (e != noErr) throw(e); } while (0)
  51.  
  52. #endif
  53.